package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.Announcement;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.*;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private CommentApi commentApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private AnnouncementsApi announcementsApi;

    public UserInfoVo findByUserInfo(String huanxinId) {
        //调用api,以huanxinId为条件查询
        User user = userApi.findByHuanxin(huanxinId);
        //获取对象userId,查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());

        UserInfoVo vo=new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);
        if(userInfo.getAge()!=null){
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    public void contacts(Long friendId) {
        //获取用户id
        Long userId = UserHolder.getUserId();
        //传入参数到环信,添加好友
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + userId,
                Constants.HX_USER_PREFIX + friendId);
        //判断是否添加成功
        if(!aBoolean){
            //添加失败,自定义异常
            throw new BusinessException(ErrorResult.error());
        }
        //添加成功,记录好友关系到MongoDB
        friendApi.save(userId,friendId);
    }

    public PageResult contactsPage(int page, int pagesize, String keyword) {
        //获取当前用户id
        Long userId = UserHolder.getUserId();
        //调用friendApi,查询
        List<Friend> friendList=friendApi.listById(userId,page,pagesize);
        //判断好友列表是否为空
        if(CollUtil.isEmpty(friendList)){
            return new PageResult();
        }
        //不为空,获取好友ids
        List<Long> friendIds = CollUtil.getFieldValues(friendList, "friendId", Long.class);
        //根据好友ids,查询好友详情,封装查询条件
        UserInfo userInfo=new UserInfo();
        userInfo.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(friendIds, userInfo);
        //创建vos对象列表
        List<ContactVo> vos=new ArrayList<>();
        //循环遍历,转化为vo对象
        for (Friend friend : friendList) {
            UserInfo info = map.get(friend.getFriendId());
            ContactVo vo = ContactVo.init(info);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,0l,vos);
    }

    /**
     * 查询用户点赞列表
     */
    public PageResult likes(Integer page, Integer pagesize) {

        CommentType commentType = CommentType.LIKE;
        return getPage(page, pagesize, commentType);
    }

    //公共方法
    public PageResult getPage(Integer page, Integer pagesize, CommentType commentType){
        //1.获取当前点赞用户id
        Long userId = UserHolder.getUserId();
        List<Comment> list = commentApi.likes(userId,page,pagesize,commentType);
        //2、提取数据列表中的好友id
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        //3、调用UserInfoAPI查询的用户详情
        List<CommentVo> vos = new ArrayList<>();
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);
        for (Comment comment : list) {
            //排除点赞自己
            if (comment.getUserId() == UserHolder.getUserId()) {
                continue;
            }
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo,comment);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0l,vos);
    }

    /**
     * 查询用户评论列表
     */
    public PageResult comments(Integer page, Integer pagesize) {
        CommentType commentType = CommentType.COMMENT;
        return getPage(page, pagesize, commentType);
    }

    /**
     * 查询用户喜欢列表
     */
    public PageResult loves(Integer page, Integer pagesize) {
        CommentType commentType = CommentType.LOVE;
        return getPage(page, pagesize, commentType);
    }

    //查询公告列表
    public PageResult announcements(Integer page, Integer pagesize) {
        List<Announcement> announcements = announcementsApi.announcements(page,pagesize);
        List<AnnouncementsVo> vos = new ArrayList<>();
        for (Announcement announcement : announcements) {
            AnnouncementsVo vo = AnnouncementsVo.init(announcement);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,0l,vos);
    }
}
